require([
    'jquery',
    'global',
    'underscore',
    'bootstrap',
    'datatables.fixedcolumns',
    'requirejs/domready!',
    'bootstrap-datepicker',
    'bootstrap-datepicker.zh-CN'], 
function($, g, _) {
	
	var select_mobile = '',
		order_table = null,
		order_fc_table = null;
	
	//Date picker
	$('.date-input').datepicker({
		autoclose : true,
		language : 'zh-CN'
	});
	$('#submit-btn').on('click', function(){
		var $form = $(this).parents('form');
		var d = {
			mobile: $form.find('input[name="mobile"]').val() || null,
			pwd: $form.find('input[name="pwd"]').val() || null
		};
		$.ajax({
			type: 'POST',
	    	url: g.context + '/order/login',
	    	contentType: 'application/json; charset=utf-8',
	    	dataType : 'json',
	    	headers: {
	    		'x-form-id' : $form.attr('id')
	    	},
	    	data: JSON.stringify(d)
		}).done(function(result){
			if(result.code == 'ACK'){
				order_table.ajax.reload(null, false);
				$('#orderListPanel').removeClass('hidden');
				$('#sms-validation-form').addClass('hidden');
				$('#mobileUser').text(result.data);
				$('#loginedDiv').removeClass('hidden');
			}
		});
	});
	
	$('#logoutBtn').on('click', function(){
		$.ajax({
			type: 'POST',
	    	url: g.context + '/order/logOut',
	    	contentType: 'application/json; charset=utf-8',
	    	dataType : 'json'
		}).done(function(result){
			if(result.code == 'ACK'){
				$('#loginedDiv').addClass('hidden');
				$('#mobileUser').text("");
				$('#sms-validation-form input').val('');
				$('#sms-validation-form').removeClass('hidden');
				$('#orderListPanel').addClass('hidden');
			}
		});
	});
	
	$('#queryBtn').on('click',function(){
		var _startDate=$('#startDate').val();
		var _endDate=$('#endDate').val();
		if(_startDate != null && _startDate.length > 0 && _endDate != null && _endDate.length > 0 && _startDate > _endDate){
			alert("开始日期要比结束日期早！");
			return;
		}
		
		order_table.ajax.reload(null, false);
	});
	
	$('#exportBtn').on('click',function(){
		var _startDate=$('#startDate').val();
		var _endDate=$('#endDate').val();
		if(_startDate != null && _startDate.length > 0 && _endDate != null && _endDate.length > 0 && _startDate > _endDate){
			alert("开始日期要比结束日期早！");
			return;
		}
		$('#orderForm').submit();
	});
	
	$('#order-table').on('click', 'tbody td', function(){
		var dt = $('#order-table').dataTable().api();
		var idx = dt.cell(this).index();
		if(!idx) return;
		var row = dt.row(idx.row).data();
		$(this).parents('tbody').find('tr.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		var $ctr = $(this).parents('.DTFC_ScrollWrapper')
			.find('.DTFC_LeftBodyLiner .DTFC_Cloned tbody tr');
		$ctr.filter('.selected').removeClass('selected');
		$($ctr.get(idx.row)).addClass('selected');
	}).on('init.dt', function(){
		order_fc_table = new $.fn.dataTable.FixedColumns(
    		$('#order-table').dataTable().api(), {
				"iLeftColumns": 1,
				"iLeftWidth": 220
    		});
    }).on('draw.dt', function(){
    	if(order_table){
    		order_table.columns.adjust();
    	}
		if(order_fc_table){
			order_fc_table.fnRedrawLayout();
		}
    }).dataTable( {
    	language: {
            url: g.context + '/assets/js/chinese.json'
        },
        dom: 'rt<"row"<"col-md-4"i><"col-md-8"p>>',
    	processing : true,
	    serverSide : true,
	    scrollX: '100%',
		scrollCollapse : true,
	    ajax: {
	    	type: 'POST',
	    	url: g.context + '/myorders',
	    	contentType: 'application/json; charset=utf-8',
	    	global: false,
	    	data: function(d){
	    		d.startDate=$('#startDate').val();
	    		d.endDate=$('#endDate').val();
	    		return JSON.stringify(d);
	    	},
	    	error: function(xhr, status, error){
	    		console.log(error);
	    	}
	    },
	    columnDefs: [ 
	     	{ targets : ['_all'], defaultContent: '&nbsp;', searchable : false }
	    ],
	    order: [[1, 'desc']],
	    columns: [{ data: 'orderNo'},
	  	        { data: 'createdAt'},
		        { data: 'vehicleOwnerName'},
		        { data: 'mobile'},
		        { data: 'carTypeCode'},
		        { data: 'productName'},
		        { data: 'productColour'},
		        { data: 'footCode'},
		        { data: 'tailCode'},
		        { data: 'descrn'}]
    });
	order_table = $('#order-table').dataTable().api();
	
});

